<template>
  <div class="header-select">
    <treeselect v-model="deptValue" :options="deptOptions" placeholder="请选择归属部门" />
  </div>
</template>

<script>
import { treeselect } from '@/api/system/dept'
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: 'HeaderSelect',
  components: { Treeselect },
  data() {
    return {
      // 部门树选项
      deptOptions: undefined
    }
  },
  created() {
    this.getTreeselect();
  },
  computed: {
    deptValue: {
      get() {
        return this.$store.state.dept.deptValue
      },
      set(val) {
        this.$store.dispatch('dept/updateDeptValue', {
          key: 'deptValue',
          value: val
        })
      }
    },
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        // 获取selectTree所需数据
        this.deptOptions = this.handleTree(response.data, "deptId");

        // 设置全局部门ID
        this.$store.dispatch('dept/updateDeptValue', {
          key: 'deptValue',
          value: response.data[0].id
        })
      });
    },
  }
}
</script>

<style lang="scss" scoped>
  .header-select {
    position: relative;
    display: inline-block;
    padding: 0 8px;
    width: 230px;
    font-size: 14px;
    vertical-align: text-bottom;
    top: -6px;
  }
</style>
